Plotly surface render test
Contents
Plotly surface render test#
Quick tests for plotly compatibility.
For JupyterLab, need additional extensions - see https://plotly.com/python/getting-started/#jupyterlab-support:
conda install -c conda-forge -c plotly jupyter-dashjupyter labextension install jupyterlab-plotly
In some cases may get partially working installation with, e.g., blank surface plots, or plots via HV only. This usually means JupyterLab needs a restart (and maybe a rebuild). For more see https://plotly.com/python/troubleshooting/
Also unstable behaviour in some Firefox builds? May be issue with add-ons? v106.0.3 on Win seems bad, but Linux versions seem OK?
Exports to HTML:
Manual from JupyterLab OK.
Read the docs may be OK, but might depend on theme, see https://github.com/readthedocs/readthedocs.org/issues/4367. For default RTD theme, ensuring
sphinx_rtd_theme >= 1.0.0in projectrequirements.txtshould make things work. Alternatively, can manually set script load per page (see link above).Jupyter book (via Sphinx), fails to render unless
_config.ymlset as per https://jupyterbook.org/en/stable/interactive/interactive.html#plotly. But… seem to be missing figs in PDF output? (Although figs via ePSproc PAD plotting routines are appearing - may be missing some additional settings in this test notebook?)
Plotly example#
From https://plotly.com/python/3d-surface-plots/#passing-x-and-y-data-to-3d-surface-plot
On blank plots or Javascript error "t is null". try a browser refresh and/or Jupyterlab restart. Seeing this frequently in testing March 2022, Firefox 98.0.2, JupyterLab 3.3.2, see end of page for other versions - running via Holoviews wrapper seems to be more robust here?
import plotly.graph_objects as go
import pandas as pd
import numpy as np
# Read data from a csv
z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')
z = z_data.values
sh_0, sh_1 = z.shape
x, y = np.linspace(0, 1, sh_0), np.linspace(0, 1, sh_1)
fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])
fig.update_layout(title='Mt Bruno Elevation', autosize=False,
width=500, height=500,
margin=dict(l=65, r=50, b=65, t=90))
fig.show()
# From https://www.tutorialspoint.com/plotly/plotly_3d_scatter_and_surface_plot.htm
# Also empty
import numpy as np
x = np.outer(np.linspace(-2, 2, 30), np.ones(30))
y = x.copy().T # transpose
z = np.cos(x ** 2 + y ** 2)
trace = go.Surface(x = x, y = y, z =z )
data = [trace]
layout = go.Layout(title = '3D Surface plot')
fig = go.Figure(data = data)
# iplot(fig)
fig.show()
Holoviews 3D surface test#
From: https://holoviews.org/gallery/demos/plotly/surface_3d.html
See also
This wrapper seems robust, and works even when Plotly native rendering is giving issues?
import numpy as np
import holoviews as hv
hv.extension('plotly')
Define data#
# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surface = hv.Surface(Z, bounds=(-5, -5, 5, 5))
Plot#
surface.opts(colorbar=True, width=500, height=500)
Plotting with coords#
BUT only supports regularly gridded coord systems (not x,y as grids). Trying to pass (X,Y,Z) in latter case gives ValueError: Surface coordinates must be 1D arrays, x and y dimension(s) were found to have multiple dimensions. Either supply 1D arrays or use the QuadMesh element for curvilinear coordinates..
xs = np.arange(-4, 4, 0.25)
ys = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(xs, ys)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surface = hv.Surface((xs, ys, Z))
surface.opts(cmap='fire', height=500, width=500)
Versions#
import scooby
scooby.Report(additional=['xarray', 'jupyterlab','plotly','holoviews'])
| Tue Nov 08 13:16:43 2022 UTC | |||||||
| OS | Linux | CPU(s) | 64 | Machine | x86_64 | Architecture | 64bit |
| RAM | 62.8 GiB | Environment | Jupyter | File system | btrfs | ||
| Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:20:46) [GCC 9.4.0] | |||||||
| xarray | 2022.3.0 | jupyterlab | 3.2.1 | plotly | 5.11.0 | holoviews | 1.15.2 |
| numpy | 1.20.3 | scipy | 1.7.1 | IPython | 7.28.0 | matplotlib | 3.4.3 |
| scooby | 0.7.0 | ||||||
March 2022 testing: Docker with jupyter/scipy-notebook base + Firefox 98.0.2.